<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="model.Room"%>
<%@page import="model.User"%>
<%@page import="model.Round"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/refreshuserlist.js"></script>
<script type="text/javascript" src="js/roomtimer.js"></script>
<title>Room</title>
</head>
<body onunload="UnloadDo();">
<%
User u = null;
if(request.getSession()!=null && (u=(User)request.getSession().getAttribute("loggeduser"))!=null){
	Room thisroom = null;
	Boolean status = null;
	String err = null;
	
	status = (Boolean)request.getAttribute("status");
	if(status != null){
	  if(status){
	    thisroom = (Room)request.getAttribute("room");
	    if(thisroom != null){
	      %>
	      <h2 class="roomname"> Welcome, <%= u.getName() %> (<a href="/hmg/LoginServlet">logout</a>)</h2>
	      <h3>This is room: <span id="roomname"><%= thisroom.getName() %></span></h3>
	      <%
	    } else {
	      %>
	      <h3 class="error">The room you are looking for does not exist. Please try again later.</h3>
	      <%
	    }
	  } else {
	    err = (String)request.getAttribute("error");
	    if(err != null){
	      %>
	      <h3 class="error"> <%= err %></h3>
	      <%
	    } else {
	      %>
	      <h3 class="error">The room you are looking for does not exist. Please try again later.</h3>
	      <%
	    }
	  }
	}
	
	if(thisroom != null){
	  %>
	  Answer the question before the time limit: <br/>
	  <div id="timer" name="<%= thisroom.getExpirationTime() %>" class="<%= thisroom.getExpirationTime() %>">
	  </div>
	  <%= thisroom.getQuestion().getQuestion() %> <br/>
	  <form method="POST" action="VoteServlet" id="vote">
	  	<table>
	  		<tr>
	  		  <td><input type="radio" name="q" id="a" value="A" onClick="submitVote()"/><%= thisroom.getQuestion().getAnswerA() %></td>
	  		</tr>
	  		<tr>
			  <td><input type="radio" name="q" id="b" value="B" onClick="submitVote()"/><%= thisroom.getQuestion().getAnswerB() %></td>
			</tr>
			
		</table>
		<input type="hidden" name="room" id="room" value="<%=thisroom.getName() %>"/>
	  </form>
	  
	  <div id="rounds">
		<p> Last Results where:</p>
		<table>
		<%  //traigo las ultimas jugadas
	 	Iterator<Round> i = ((ArrayList<Round>)thisroom.getRounds()).iterator();
		while(i.hasNext()){
		  Round round = (Round)i.next();
		  if(round.isClosed()){
			  %>
				<tr>
			  		<td><%=round.getResult()  %></td>
				</tr>
			  <% 
			  }
		}
		%>
		</table>
		</div>
	<%
	}
	%>
	<div id="userlist"></div>
	<%
} else {
  %>
  <h3 class="error">You must be logged in to play the game.</h3> <a href="/hmg/IndexServlet">Log in now!</a>
  <%
}
	%>
</body>
</html>